/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.product.manage.service;

import cn.hutool.core.bean.BeanUtil;
import info.peigen.hotpot.business.product.core.repository.entity.ProductBrandEntity;
import info.peigen.hotpot.business.product.manage.convertor.ProductBrandManageConvertor;
import info.peigen.hotpot.business.product.manage.form.ProductBrandManageCreateForm;
import info.peigen.hotpot.business.product.manage.form.ProductBrandManageEditForm;
import info.peigen.hotpot.business.product.manage.form.ProductBrandManageSearchForm;
import info.peigen.hotpot.business.product.manage.info.ProductBrandManageInfo;
import info.peigen.hotpot.business.product.manage.repository.service.ProductBrandManageRepository;
import info.peigen.hotpot.common.core.result.PageResult;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

/**
 * <b>(ProductBrandManageService)</b>
 * 管理后台 "商品品牌" 服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-05-02
 */
@SuppressWarnings("unused")
@Component
public class ProductBrandManageService {
    @Inject
    ProductBrandManageRepository productBrandManageRepository;

    public PageResult<ProductBrandManageInfo> page(ProductBrandManageSearchForm form) {
        LambdaQuery<ProductBrandEntity> lambdaQuery = productBrandManageRepository.getMapper().createLambdaQuery()
                .andLike(ProductBrandEntity::getBrandId, Query.filterLikeEmpty(form.getBrandId(), false, true))
                .andLike(ProductBrandEntity::getName, Query.filterLikeEmpty(form.getName(), false, true))
                .andLike(ProductBrandEntity::getSelf, Query.filterLikeEmpty(form.getSelf(), false, true))
                .andLike(ProductBrandEntity::getPartnerId, Query.filterLikeEmpty(form.getPartnerId(), false, true));

        return productBrandManageRepository.page(form.getPageNum(), form.getPageSize(), lambdaQuery, ProductBrandManageConvertor::convertToInfo);
    }

    public void create(ProductBrandManageCreateForm form) {
        ProductBrandEntity entity = BeanUtil.copyProperties(form, ProductBrandEntity.class);
        productBrandManageRepository.save(entity);
    }

    public void edit(ProductBrandManageEditForm form) {
        ProductBrandEntity entity = BeanUtil.copyProperties(form, ProductBrandEntity.class);
        productBrandManageRepository.saveOrUpdate(entity);
    }

    public void remove(Integer id) {
        productBrandManageRepository.delete(id);
    }
}